<template>
  <Header :title="{ zh: '裁剪目视管理看板', en: 'Cutting & Preparation Visual Board', index: 12 }" />

  <div class="EchartsContainer">
    <div class="row">
      <div class="item" style="width: 40%">
        <div class="title">周产能状况<span style="color: red;">(双)</span></div>
        <div class="echarts" id="main"></div>
      </div>
      <div class="item" style="width: 40%">
        <div class="title">月产能状况<span style="color: red;">(双)</span></div>
        <div class="echarts" id="main2"></div>
      </div>
      <div class="item" style="width: 40%">
        <div class="title">裁剪库存状况<span style="color: red;">(双)</span></div>
        <div class="echarts" id="main3"></div>
      </div>

    </div>

    <div class="row" style="margin-top: 10px">
      <swiper :autoplay="{
        delay: 60000
      }" :slides-per-view="1" :space-between="2000" style="height: 100%; width: 100%">
        <swiper-slide>
          <div class="item">
            <div class="title">（电雕机）稼动率</div>
            <div class="echarts" id="main4"></div>
          </div>
        </swiper-slide>
        <swiper-slide>
          <div class="item">
            <div class="title">（切割机）稼动率</div>
            <div class="echarts" id="main5"></div>
          </div>
        </swiper-slide>
      </swiper>

    </div>
  </div>
</template>

<script setup lang="ts">
import { nextTick, onBeforeUnmount, onMounted, ref } from 'vue'
import Header from '../components/Header.vue'
import * as echarts from 'echarts'
import { ElMessage } from 'element-plus'
import request from '@/utils/request'
import { Swiper, SwiperSlide } from 'swiper/vue'

// let rollTimer = ref() as any

// const tab = ref<HTMLElement>() as any
// const autoRoll = (stop: any) => {
//   if (stop) {
//     clearInterval(rollTimer.value)
//     return
//   }
//   // 这里的 tab 是上方 table 表格绑定的ref值
//   const divData = tab.value.layout.table.refs.bodyWrapper.firstElementChild.firstElementChild
//   rollTimer.value = setInterval(() => {
//     divData.scrollTop += 1
//     // console.log(divData.scrollTop)
//     if (divData.clientHeight + divData.scrollTop == divData.scrollHeight) {
//       divData.scrollTop = 0
//     }
//   }, 150)
// }
// onMounted(() => {
//   autoRoll(false)
// })
// onBeforeUnmount(() => {
//   autoRoll(true)
// })

const showData = ref({
  chart1: {
    xData: [],
    data: [
      {
        name: '裁剪',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [9494, 10820, 10602, 7320, 0, 0]
      },
      {
        name: '配套',
        type: 'bar',
        barGap: 0,
        label: {
          show: true,
          position: 'top'
        },
        emphasis: {
          focus: 'series'
        },
        data: [9408, 11022, 10210, 6893, 0, 0]
      },
    ]
  },
  chart2: {
    data: []
  },
  chart3: {
    data: []
  },

}) as any
// 获取针车
const DeptNos = ref([]) as any
const DeptName = ref([]) as any
const getDeptTypes = async () => {


}


// 获取成型
const DeptNos2 = ref([]) as any
const DeptName2 = ref([]) as any
const getDeptTypes2 = async () => {
  // try {
  //   let tableDataList = (await request.put<any, any>(
  //     `/DeptMgt/deptTemp/back?deptType=${'配套'}`
  //   )) as any
  //   tableDataList.forEach((item: any) => {
  //     DeptNos2.value.push(item.deptNo)
  //     DeptName2.value.push(item.deptName)
  //   })
  // } catch (error: any) {
  //   ElMessage.error(error)
  // }
}
// 折线图
const monthData = ref([
  {
    xData: [],
    Data: []
  },
  {
    xData: [],
    Data: []
  },
]) as any


const getData = async () => {
  // try {
  //   // 获取当月开关机数据
  //   let macName = encodeURIComponent('1#电雕机')
  //   let macName2 = encodeURIComponent('1#切割机')
  //   // let tableDataList = (await request.post<any, any>(
  //   //   `/macMgt/show/query/showMonQuery?macType=${''}&macName=${macName}`
  //   // )) as any
  //   // tableDataList.forEach((item: any, index: any) => {
  //   //   monthData.value[0].xData.push(item.reDate)
  //   //   monthData.value[0].Data.push(item.rate)
  //   // })
  //   let tableDataList2 = (await request.post<any, any>(
  //     `/macMgt/show/query/showMonQuery?macType=${''}&macName=${macName2}`
  //   )) as any
  //   tableDataList2.forEach((item: any, index: any) => {
  //     monthData.value[1].xData.push(item.reDate)
  //     monthData.value[1].Data.push(item.rate)
  //   })
  // } catch (error: any) {
  //   ElMessage.error(error)
  // }
}


onMounted(async () => {
  await getDeptTypes()
  await getDeptTypes2()
  await getData()
  getEchartsDom()
})
const getEchartsDom = () => {
  let chartDom = document.getElementById('main')
  let myChart = echarts.init(chartDom)
  let app = {} as any
  const posList = [
    'left',
    'right',
    'top',
    'bottom',
    'inside',
    'insideTop',
    'insideLeft',
    'insideRight',
    'insideBottom',
    'insideTopLeft',
    'insideTopRight',
    'insideBottomLeft',
    'insideBottomRight'
  ]
  app.configParameters = {
    rotate: {
      min: -90,
      max: 90
    },
    align: {
      options: {
        left: 'left',
        center: 'center',
        right: 'right'
      }
    },
    verticalAlign: {
      options: {
        top: 'top',
        middle: 'middle',
        bottom: 'bottom'
      }
    },
    position: {
      options: posList.reduce(function (map: any, pos) {
        map[pos] = pos
        return map
      }, {})
    },
    distance: {
      min: 0,
      max: 100
    }
  }
  app.config = {
    rotate: 90,
    align: 'left',
    verticalAlign: 'middle',
    position: 'insideBottom',
    distance: 15,
    onChange: function () {
      const labelOption = {
        rotate: app.config.rotate,
        align: app.config.align,
        verticalAlign: app.config.verticalAlign,
        position: app.config.position,
        distance: app.config.distance
      }
      myChart.setOption({
        series: [
          {
            label: labelOption
          },
          {
            label: labelOption
          },
          {
            label: labelOption
          },
          {
            label: labelOption
          }
        ]
      })
    }
  }
  let option = {
    color: ['#72DDB3', '#93BEFF'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    grid: {
      left: 50,
      right: 0,
      top: 30,
      bottom: 45
    },
    legend: {
      data: ['裁剪', '配套'],
      top: 'bottom'
    },
    xAxis: [
      {
        type: 'category',
        axisTick: { show: false },
        data: ['星期一', '星期二', '星期三', '星期四', '星期五', '星期六']
        // data: showData.value.chart1.xData
      }
    ],
    yAxis: [
      {
        type: 'value'
      }
    ],
    series: showData.value.chart1.data
  }
  option && myChart.setOption(option)


  // 第二张图
  let chartDom2 = document.getElementById('main2')
  let myChart2 = echarts.init(chartDom2)
  let option2 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    grid: {
      left: 50,
      right: 0,
      top: 30,
      bottom: 45
    },
    xAxis: [
      {
        type: 'category',
        data: ['裁剪', '配套'],
        axisPointer: {
          type: 'shadow'
        }
      }
    ],
    yAxis: [
      {
        type: 'value',
        min: 0,
        // max: 1000,
        // interval: 200,
        axisLabel: {
          formatter: '{value}'
        }
      }
    ],
    series: [
      {
        type: 'bar',
        label: {
          show: true,
          position: 'top'
        },
        barWidth: '15%',
        tooltip: {
          valueFormatter: function (value: any) {
            return value
          }
        },
        itemStyle: {
          color: new echarts.graphic.LinearGradient(
            0,
            0,
            0,
            1, // 渐变方向：从上到下
            [
              { offset: 0, color: '#3662EC' },
              { offset: 1, color: 'white' }
            ]
          )
        },
        data: [38236, 37533]
        // data: showData.value.chart2.data
      }
    ]
  }
  option2 && myChart2.setOption(option2)

  // 第三张图
  let chartDom3 = document.getElementById('main3')
  let myChart3 = echarts.init(chartDom3)
  let option3 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        crossStyle: {
          color: '#999'
        }
      }
    },
    grid: {
      left: 70,
      right: 0,
      top: 30,
      bottom: 45
    },
    xAxis: [
      {
        type: 'category',
        axisTick: { show: false },
        data: ['总库存', '配套库存']
      }
    ],
    yAxis: [
      {
        type: 'value',
      }
    ],
    series: {
      type: 'bar',
      label: {
        show: true,
        position: 'top'
      },
      barWidth: '15%',
      tooltip: {
        valueFormatter: function (value: any) {
          return value
        }
      },
      itemStyle: {
        color: new echarts.graphic.LinearGradient(
          0,
          0,
          0,
          1, // 渐变方向：从上到下
          [
            { offset: 0, color: '#FA7358' },
            { offset: 1, color: 'white' }
          ]
        )
      },
      data: [189000, 107000,]
      // data: showData.value.chart3.data
    }
  }
  option3 && myChart3.setOption(option3)


  let chartDom4 = document.getElementById('main4')
  let myChart4 = echarts.init(chartDom4)
  let option4 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      top: 'bottom'
    },
    grid: {
      left: 20,
      right: 20,
      bottom: 40,
      top: 30,
      containLabel: true
    },
    xAxis: {
      type: 'category',
      // data: monthData.value[0].xData
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '稼动率',
        // data: monthData.value[0].Data,
        data: [0.345, 0.678, 0.901, 0.123, 0.456, 0.789, 0.234, 0.567, 0.890, 0.345],
        type: 'line',
        label: {
          show: true,
          position: 'top'
        },
        color: '#FAC858'
      }
    ]
  }
  option4 && myChart4.setOption(option4)


  let chartDom5 = document.getElementById('main5')
  let myChart5 = echarts.init(chartDom5)
  let option5 = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow'
      }
    },
    legend: {
      top: 'bottom'
    },
    xAxis: {
      type: 'category',
      // data: monthData.value[1].xData
    },
    grid: {
      left: 20,
      right: 20,
      bottom: 40,
      top: 30,
      containLabel: true
    },
    yAxis: {
      type: 'value'
    },
    series: [
      {
        name: '稼动率',
        // data: monthData.value[1].Data,
        data: [0.234, 0.567, 0.123, 0.890, 0.456, 0.789, 0.345, 0.678, 0.901, 0.234],
        type: 'line',
        label: {
          show: true,
          position: 'top'
        },
        color: '#72DDB3'
      }
    ]
  }
  option5 && myChart5.setOption(option5)
}

// 表格数据
const tableData = ref([])
onMounted(() => {
  function One() {
    if (location.href.indexOf('#reloaded') == -1) {
      location.href = location.href + '#reloaded'
      location.reload()
    }
  }
  One()
})
</script>

<style scoped lang="less">
.EchartsContainer {
  border-radius: 4px;
  margin-top: 10px;
  height: calc(100vh - 7vh - 40px);
  width: 100%;
}

.row {
  display: flex;
  height: 50%;
  width: 100%;

  .item {
    background-color: white;
    width: 100%;
    display: flex;
    flex-direction: column;
    height: 100%;
    padding-bottom: 10px;
    padding: 0 10px 5px;

    // align-items: center;
    .title {
      font-weight: 900;
      font-size: 1rem;
      margin-top: 10px;
      text-align: center;
      color: black;
    }

    .echarts {
      flex: 1;
    }
  }
}

:deep(thead) {
  .is-center {
    color: white;
    background-color: #ff9940;
  }
}
</style>
